import {customRef} from "vue"

export default function(initValue:string,delay:number){
    let timer:number
    let msg = customRef((track,trigger)=>{
        return {
            //msg被读取时，调用get()
            get(){
                track()     //告诉vue要对msg进行持续的关注，一旦msg变化了就更新
                return initValue
            },
            //修改msg时，调用set()
            set(value){
                clearTimeout(timer)
                timer = setTimeout(()=>{
                    initValue = value
                    trigger()       //告诉vue：msg变化了
                },delay)
            }
        }
    })
    return {msg}
}